/**
  ******************************************************************************
  * @file    TIM/7_PWMOutputs/stm32f0xx_it.c 
  * @author  MCD Application Team
  * @version V1.2.0
  * @date    22-November-2013
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and 
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_it.h"
//uint16_t capture = 0;
//extern __IO uint16_t CCR1_Val;
//extern __IO uint16_t CCR2_Val;
//extern __IO uint16_t CCR3_Val;
//extern __IO uint16_t CCR4_Val;

/** @addtogroup STM32F0xx_StdPeriph_Examples
  * @{
  */

/** @addtogroup TIM_7_PWMOutputs
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M0 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
	extern __IO uint16_t NowCounter;
void SysTick_Handler(void)
{
	NowCounter++;
}

/******************************************************************************/
/*                 STM32F0xx Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f0xx.s).                                               */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

//#define DBG_printf	printf
#define DBG_printf(...)	//

void WriteCallBackToMCU(unsigned char);

#define I2C_MAX_SZ	(128)
uint16_t jTX = 0;
uint16_t jRX = 0;
uint8_t DataRx[I2C_MAX_SZ];//DataRx[512];
uint8_t DataTx[I2C_MAX_SZ];//DataTx[512];
uint8_t u8RxCnt;
uint32_t I2C1_IRQHandler(void) {

  // on each request-from-I2C-Master, I2C_ADDR was triggered/called, thus the jTX variable was resetted
  if (I2C_GetITStatus(I2C1, I2C_IT_ADDR)) {
		
		 if(I2C_GetTransferDirection(I2C1)==I2C_Direction_Transmitter)
		 {
			 //Master to Slave
			 jRX = 0;
			 jTX = 0;
				
				UartPuts("\r\n@:S,A+W,");
				DBG_printf("\r\n#%xS,A+W,",u8RxCnt++);	
		 }
		 else
		 {
			  //I2C_SendData(I2C1,0xff);//dummy byte//No funciton
				UartPuts("\r\n@:S,A+R,");
				DBG_printf("\r\n#%xS,A+R,",u8RxCnt++);
							
				
		 }
     

     I2C_ClearITPendingBit(I2C1, I2C_IT_ADDR);
     I2C_ClearFlag(I2C1,I2C_FLAG_ADDR);
		 

     return 0;
  }

  if (I2C_GetITStatus(I2C1, I2C_IT_RXI)) {
     I2C_ClearITPendingBit(I2C1, I2C_IT_RXI);
		 
		 //if(jRX<sizeof(DataRx))
		 if(jRX<I2C_MAX_SZ)
		 
		 {
			 DataRx[jRX]=I2C_ReceiveData(I2C1);
			 //DBG_printf("%2x,",DataRx[jRX]);//debug
			 
			 WriteCallBackToMCU(jRX);
			 
			 jRX++;
		 }
		 else
		 {
			 I2C_ReceiveData(I2C1);//FIFO FUll
		 }
		 
		
     return 0;
  }

  // on the first (2 byte) request, the I2C_IT_TXIS below was triggered/called 3 times !!
  //   so this I2C-slave-unit sent 1,2,3 (only 1 and 2 received by the I2C-master)
  // on the next 2 byte request from I2C-Master, it only triggered/called twice (sending 1 and 2)
  // and the I2C-Master received 3 (from last batch) and 1 from this batch
  if (I2C_GetITStatus(I2C1, I2C_IT_TXIS)) {
      
			if(jTX<sizeof(DataTx))
			{
				I2C_SendData(I2C1,DataTx[jTX]);
				//DBG_printf("%2x.",DataTx[jTX]);//debug
				
				jTX++;
			}
			else
			{
				I2C_SendData(I2C1,0xff);//FIFO FUll
			}
			
   return 0;
  }

  if (I2C_GetITStatus(I2C1, I2C_IT_TCR)) {
     I2C_ClearITPendingBit(I2C1, I2C_IT_TCR);
     I2C_ClearFlag(I2C1,I2C_IT_TCR);
		 DBG_printf("TCR?");
     return 0;
  }

  if (I2C_GetITStatus(I2C1, I2C_IT_STOPI)) {
     I2C_ClearITPendingBit(I2C1, I2C_IT_STOPI);
     I2C_ClearFlag(I2C1,I2C_FLAG_STOPF);
		 
		 UartPuts("P@");
		 DBG_printf("P,");
     return 0;
  }

  if (I2C_GetITStatus(I2C1, I2C_IT_ALERT)) {
    I2C_ClearITPendingBit(I2C1, I2C_IT_ALERT);
		DBG_printf("AL?");
    return 0;
  }

  /* Check on I2C1 Time out flag and clear it */
  if (I2C_GetITStatus(I2C1, I2C_IT_TIMEOUT)) {
    I2C_ClearITPendingBit(I2C1, I2C_IT_TIMEOUT);
		DBG_printf("TO?");
    return 0;
  }

  /* Check on I2C1 Arbitration Lost flag and clear it */
  if (I2C_GetITStatus(I2C1, I2C_IT_ARLO)) {
    I2C_ClearITPendingBit(I2C1, I2C_IT_ARLO);
		DBG_printf("AR?");
    return 0;
  }

  /* Check on I2C1 PEC error flag and clear it */
  if (I2C_GetITStatus(I2C1, I2C_IT_PECERR)) {
    I2C_ClearITPendingBit(I2C1, I2C_IT_PECERR);
		DBG_printf("PE?");
    return 0;
  }

  /* Check on I2C1 Overrun/Underrun error flag and clear it */
  if (I2C_GetITStatus(I2C1, I2C_IT_OVR)) {
    I2C_ClearITPendingBit(I2C1, I2C_IT_OVR);
		DBG_printf("OV?");
    return 0;
  }

  /* Check on I2C1 Acknowledge failure error flag and clear it */
  if (I2C_GetITStatus(I2C1, I2C_IT_NACKF)) {
    I2C_ClearITPendingBit(I2C1, I2C_IT_NACKF);
		//DBG_printf("NK?");
    return 0;
  }

  /* Check on I2C1 Bus error flag and clear it */
  if (I2C_GetITStatus(I2C1, I2C_IT_BERR)) {
    I2C_ClearITPendingBit(I2C1, I2C_IT_BERR);
		DBG_printf("BE?");
    return 0;
  }
  return 0;
}


/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
